import { isNumber } from "../../utils/check";

const defalutData = {
  name: "",
  price: "",
  priceEarly: "",
  priceDiscount: "",
  stock: "",
};

Component({
  properties: {
    editable: { type: Boolean, value: false },
    arrayData: { type: Array, value: [] },
    // deposit: { type: String, value: "" },
    type: { type: String, value: "standard" },
    date: { type: String, value: "" },
  },

  data: {
    popupVisible: false,
    editingIndex: -1, // 当前编辑的索引, -1表示新增
    // depositInput: "",
    formData: {
      ...defalutData,
    },
    tableHeaders: [
      { label: "票名", key: "name" },
      { label: "正价", key: "price" },
      { label: "提前报名", key: "priceEarly" },
      { label: "女生优惠", key: "priceDiscount" },
      { label: "库存", key: "stock" },
    ],
  },
  // observers: {
  //   deposit: function (newVal) {
  //     this.setData({
  //       depositInput: newVal,
  //     });
  //   },
  // },
  methods: {
    // 显示新增弹框
    onAdd() {
      this.setData({
        editingIndex: -1,
        popupVisible: true,
        formData: {
          ...defalutData,
        },
      });
    },

    // 输入框
    onInputChange(e) {
      const { field } = e.currentTarget.dataset;
      console.log("---field------", field, e.detail.value);
      this.setData({
        [`${field}`]: e.detail.value,
      });
    },

    onRowTap(e) {
      if (!this.properties.editable) {
        return;
      }
      const index = e.detail.index;
      this.setData({
        editingIndex: index,
        popupVisible: true,
        formData: { ...this.data.arrayData[index] },
      });
    },

    // 请求删除
    onDelete(e) {
      const index = e.detail.index;
      wx.showModal({
        title: "确认删除",
        content: "您确定要删除这条收费信息吗？",
        success: (res) => {
          if (res.confirm) {
            this.triggerEvent("delete", { index });
          }
        },
      });
    },

    // 保存
    onSubmit() {
      const { editingIndex } = this.data;
      const data = this.validateForm();

      if (!data) {
        return;
      }
      this.triggerEvent("save", {
        index: editingIndex,
        data,
      });
      this.onCancel();
    },

    // 取消
    onCancel() {
      this.setData({ popupVisible: false });
    },

    validateForm() {
      const { formData } = this.data;
      const array = [
        { key: "name", title: "请输入票名" },
        { key: "price", title: "请输入价格" },
        { key: "stock", title: "请输入库存" },
      ];
      const data = {};
      for (let index = 0; index < array.length; index++) {
        const item = array[index];
        const { key, title } = item;
        if (!formData[key] || formData[key].trim() === "") {
          wx.showToast({
            title,
          });
          return false;
        }
        data[key] = formData[key].trim();
      }
      // 数字验证
      const arr = ["price", "stock", "priceEarly", "priceDiscount"];
      for (let index = 0; index < arr.length; index++) {
        const key = arr[index];
        const value = formData[key];
        if (value && !isNumber(value, 9999)) {
          wx.showToast({
            title: "请输入0-9999间的整数",
            icon: "none",
          });
          return false;
        }
        data[key] = Number(value);
      }
      const { date, type } = this.properties;
      data.date = date;
      data.type = type;
      return data;
    },
  },
});
